package com.gaogzhen.intro;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> task = new FutureTask<>(() -> "~~~~我在敲代码~~~~");
        new Thread(task).start();
        String s = task.get();
        System.out.println(s);

        Date start = new Date();
        // 创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future<String>> ret = new ArrayList<>(5);

        for (int i = 0; i < 5; i++) {
            Callable<String> call = new MyCallable();
            // 提交任务
            Future<String> future = executorService.submit(call);
            ret.add(future);
        }

        // 关闭线程池
        executorService.shutdown();

        // 获取返回结果
        for (Future<String> f: ret) {
            System.out.println(f.get());
        }
        Date end = new Date();
        System.out.println(Thread.currentThread().getName()+"程序结束，执行用时："+(end.getTime() - start.getTime()) + "毫秒");
    }
}
